import { segToPolygonPoints } from './polar.js'
import { controlPoints } from './controlPoints.js'
import { _k, _polar, _rotate, _random } from '../math/index.js'



export const gouguPoints = (options) => {
    let matrix = []
    let { o, n = 4, a = 180, r = 100, depth = 3, wriggle = 10 } = options
    let from = o

    let to = _polar(o, r, a)
    let points = segToPolygonPoints([from, to], n, true)
    matrix.push(points)

    const _ra = () => {
        return wriggle ? _random(-wriggle, wriggle) : 0
    }

    const _gougu = (points, i, depth) => {
        let t = points[i]
        let next = points[(i + 1) % n]
        let [c1, c2] = controlPoints([t, next], -180 + _ra())

        let ps = segToPolygonPoints([c1, t], n)
        matrix.push(ps)
        if (depth > 0) {
            _gougu(ps, Math.floor(n / 2), depth - 1)
        }

        let ps2 = segToPolygonPoints([next, c1], n)
        matrix.push(ps2)
        if (depth > 0) {
            _gougu(ps2, Math.ceil(n / 2), depth - 1)
        }
    }
    _gougu(points, 0, depth)

    return matrix

}